package com.example.demo.service;

import com.example.demo.dao.mapper.SysTokenMapper;
import com.example.demo.dao.model.SysToken;
import com.example.demo.dao.model.SysTokenCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TokenService implements ITokenService {

    @Autowired
    private SysTokenMapper sysTokenMapper;

    @Override
    public Boolean insertToken(SysToken token) {
        int count = sysTokenMapper.insertSelective(token);
        return count > 0;
    }

    @Override
    public Boolean updateToken(SysToken token) {

        int count = sysTokenMapper.updateByPrimaryKeySelective(token);
        return count > 0;
    }

    @Override
    public SysToken selectOne(String tokenId) {
        try {
            Integer id = Integer.parseInt(tokenId);
            return sysTokenMapper.selectByPrimaryKey(id);
        } catch (Exception e) {
            // ignore
        }

        return null;
    }

    @Override
    public List<SysToken> selectMany(SysToken tokenCriteria) {
        SysTokenCriteria criteria = new SysTokenCriteria();
        criteria.createCriteria()
                .andSessionIdEqualTo(tokenCriteria.getSessionId());

        // sessionId = parameter sessionId
        return this.sysTokenMapper.selectByCriteria(criteria);
    }
}
